#include <51func.h>
#define NUM 10000000
int global = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//锁的静态初始化
void *threadFunc(void *arg){
    for(int i = 0; i < 10000000; ++i){
        pthread_mutex_lock(&mutex); // 临界区开始
        ++global;
        pthread_mutex_unlock(&mutex); // 临界区结束
    }
    pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
    pthread_t tid;
    // gettimeofday
    struct timeval begTime,endTime;
    gettimeofday(&begTime,NULL);
    pthread_create(&tid,NULL,threadFunc,NULL);
    for(int i = 0; i < 10000000; ++i){
        pthread_mutex_lock(&mutex); // 临界区开始
        ++global;
        pthread_mutex_unlock(&mutex); // 临界区结束
    }
    pthread_join(tid,NULL);
    // gettimeofday
    gettimeofday(&endTime,NULL);
    printf("total = %d\n", global);
    printf("total time = %ld us\n", endTime.tv_usec - begTime.tv_usec + 1000000*(endTime.tv_sec - begTime.tv_sec));
    return 0;
}

